package com.mybatis3.demo.dao.impl;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis3.demo.dao.UserDao;
import com.mybatis3.demo.mapper.UserMapper;
import com.mybatis3.demo.pojo.GroupEntity;
import com.mybatis3.demo.pojo.UserEntity;

public class UserDaoImpl implements UserDao {

	SqlSessionFactory sqlSessionFactory;
	
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public void addUser(UserEntity entity) {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = null;
		try {
			userMapper = session.getMapper(UserMapper.class);
			userMapper.addUser(entity);
			session.commit();
		}catch(Exception e){
			session.rollback();
			throw e;
		}finally {
			session.close();
		}
	}

	@Override
	public void deleteUser(UserEntity entity) {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = null;
		try {
			userMapper = session.getMapper(UserMapper.class);
			userMapper.deleteUser(entity);
			session.commit();
		}catch(Exception e){
			session.rollback();
			throw e;
		}finally {
			session.close();
		}
	}

	@Override
	public UserEntity findUserByEmail(String email) {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = null;
		UserEntity userEntity = null;
		try {
			userMapper = session.getMapper(UserMapper.class);
			userEntity = userMapper.findUserByEmail(email);
		}catch(Exception e){
			throw e;
		}finally {
			session.close();
		}
		return userEntity;
	}

	@Override
	public List<UserEntity> findUsers() {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = null;
		List<UserEntity> list = null;
		try {
			userMapper = session.getMapper(UserMapper.class);
			list = userMapper.findUsers();
		} catch(Exception e){
			throw e;
		}finally {
			session.close();
		}
		return list;
	}
	
	@Override
	public void updateUser(UserEntity entity) {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = null;
		try {
			userMapper = session.getMapper(UserMapper.class);
			userMapper.updateUser(entity);
			session.commit();
		}catch(Exception e){
			session.rollback();
			throw e;
		}finally {
			session.close();
		}
	}

	@Override
	public UserEntity findUserByEmailAndPass(String email, String password) {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper userMapper = null;
		UserEntity userEntity = null;
		try {
			userMapper = session.getMapper(UserMapper.class);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("email", email);
			map.put("password", password);
			userEntity = userMapper.findUserByEmailAndPass(map);
		}catch(Exception e){
			throw e;
		}finally {
			session.close();
		}
		return userEntity;
	}

	@Override
	public UserEntity findByContions(UserEntity entity) {
		SqlSession session = sqlSessionFactory.openSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		UserEntity user = mapper.findByContions(entity);
		session.close();
		return user;
	}
	
	public static void main(String[] args) throws IOException {
		String resources = "com/mybatis3/demo/config/mybatis.cfg.xml";
		Reader reader = Resources.getResourceAsReader(resources);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		
		UserDaoImpl userDaoImpl = new UserDaoImpl();
		userDaoImpl.setSqlSessionFactory(sqlSessionFactory);
		
		GroupEntity group = new GroupEntity();
		group.setName("教师");
		
		UserEntity entity = new UserEntity();
		//entity.setEmail("ywl@qq.com");
		entity.setAge(18);
		entity.setGroupEntity(group);
		
		UserEntity user = userDaoImpl.findByContions(entity);
		System.out.println(user);
		
		/*UserEntity userEntity = userDaoImpl.findUserByEmail("ywl@qq.com");
		System.out.println(userEntity);*/
		
		/*UserEntity entity = new UserEntity();
		entity.setEmail("xm@qq.com");
		entity.setPassword("xm");
		entity.setSex("女");
		entity.setAge(24);
		
		userDaoImpl.addUser(entity);*/
		
		/*List<UserEntity> list = userDaoImpl.findUsers();
		for (UserEntity userEntity : list) {
			System.out.println(userEntity);
		}*/
		
		/*UserEntity entity = userDaoImpl.findUserByEmail("123");
		entity.setAge(21);
		entity.setPassword("111");
		entity.setSex("女");
		
		userDaoImpl.updateUser(entity);*/
		
		/*UserEntity entity = userDaoImpl.findUserByEmail("123");
		userDaoImpl.deleteUser(entity);*/
		
		/*UserEntity entity = userDaoImpl.findUserByEmailAndPass("ywl@qq.com", "ywl");
		System.out.println(entity);*/
	}

}
